import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import User from '../views/User.vue'
import Mail from '../views/Mail.vue'
import PageOne from '../views/PageOne.vue'
import PageTwo from '../views/PageTwo.vue'
import Login from '../views/Login.vue'
import Person from '../views/Person.vue'
import Main from '../views/Main.vue'//导入组件

Vue.use(VueRouter)
//将路由与组件映射
const routes = [
  {//主路由
    path:'/',
    component:Main,
    redirect:'/home',//重定向
    children:[//子路由 
      {//首页
        path: 'home',
        name: 'home',
        component: Home
      },
      {//用户管理
        path: 'user', 
        name: 'user',
        component: User 
      },
      {//商品管理
        path: 'mail',
        name: 'mail',
        component: Mail
      },
      {//页面1
        path: 'page1',
        name: 'page1',
        component: PageOne
      },
      {//页面2
        path: 'page2',
        name: 'page2',
        component: PageTwo
      },
      {//个人信息
        path: 'person',
        name: 'person',
        component: Person
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component:Login
    
  }
  
  ]

const router = new VueRouter({
    routes
})

// //添加全局前置导航守卫
// router.beforeEach((to, from , next) => {
//     const token = localStorage.getItem('token')

//     if(!token && to.name!=='login'){//token不存在，用户未登录
//         next({name : 'login'})
//     }else if(token && to.name == 'login'){//有token不能去login？
//       next({ name:from.name })
//     }else{
//       next()
//     }
// })

export default router

